package com.example.teesystem.entityDto;

import com.example.teesystem.common.cache.KnowledgeCache;
import com.example.teesystem.common.utils.IssueUtil;
import com.example.teesystem.entity.Issue;
import com.example.teesystem.entity.IssueRecord;
import com.example.teesystem.entity.Knowledge;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : NieYaShi
 * @version : v1.0
 * @createTime : 2022/3/31 19:10
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class IssueList {
    private Integer id;

    private String question;

    private String knowledgeList;

    private Integer difficulty;

    private Integer chapterId;

    private String type;

    private Integer referenceNum;

    private List<Knowledge> knowledges = new ArrayList<>();

    private float errorRate = 0;

    public IssueList(Issue issue, List<IssueRecord> issueRecordList){
        BeanUtils.copyProperties(issue, this);
        this.type = IssueUtil.getIssueTypeName(issue.getType());

        if (knowledgeList != null && !knowledgeList.equals("")) {
            String[] knowledgeIds = knowledgeList.split("\\$");
            Knowledge def = new Knowledge();
            def.setMessage("");
            for (String knowledgeId : knowledgeIds) {
                Knowledge knowledge = KnowledgeCache.knowledgeMap.getOrDefault(Integer.parseInt(knowledgeId), def);
                knowledges.add(knowledge);
            }
        }

        if (issueRecordList.size() != 0){
            float rightNum = 0;
            float wrongNum = 0;
            for (IssueRecord issueRecord : issueRecordList){
                rightNum += issueRecord.getRightNum();
                wrongNum += issueRecord.getWrongNum();
            }
            this.errorRate = wrongNum / (rightNum + wrongNum);
        }else {
            this.errorRate = 0f;
        }
    }
}
